package com.toune.dltools.ui

import android.R.attr.data
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import com.blankj.rxbus.RxBus
import com.tamsiree.rxkit.view.RxToast
import com.toune.dltools.R
import com.toune.dltools.bean.DLCreateFormBean
import kotlinx.android.synthetic.main.activity_add_form_list_item.*
import kotlinx.android.synthetic.main.dialog_create_form.view.*


class DLAddFormListItemActivity : DLBaseActivity<DLBaseViewModel>() {
    override val layout: Int
        get() = R.layout.activity_add_form_list_item
    override val titleStr: String?
        get() = "添加条目"


    val dataBeans = ArrayList<String>()
    var adapter: ArrayAdapter<String>? = null
    var tag = "0"
    override fun init(savedInstanceState: Bundle?) {
        tag = intent.extras!!.getString("tag")!!
        when (tag) {
            DLCreateFormBean.SPINNER.toString() -> {
                hintLv.visibility = View.VISIBLE
            }
            else -> {
                hintLv.visibility = View.GONE
            }
        }
        val addRightView = addRightView("保存")
        addRightView!!.setOnClickListener {
            if (nameEt.text!!.isBlank()) {
                RxToast.info("输入名称")
                return@setOnClickListener
            }
            if (hintLv.visibility == View.VISIBLE && placeHolderEt.text!!.isBlank()) {
                RxToast.info("输入提示语")
                return@setOnClickListener
            }
            val dlCreateFormBean = DLCreateFormBean()
            dlCreateFormBean.tag = tag.toInt()
            dataBeans.add(nameEt.text.toString())
            if (placeHolderEt.text.toString().isNotBlank()) {
                dataBeans.add(placeHolderEt.text.toString())
            }
            dlCreateFormBean.params = dataBeans
            RxBus.getDefault().post(dlCreateFormBean)
            finish()
        }
        setAdapter()
    }

    private fun setAdapter() {
        if (adapter != null) {
            adapter!!.notifyDataSetChanged()
        } else {
            adapter = ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1, dataBeans
            )
            listView.adapter = adapter
        }
    }

    override fun initEventAndData() {
        addBtn.setOnClickListener {
            if (inputEt.text.toString().isNotBlank()) {
                dataBeans.add(inputEt.text.toString())
                inputEt.setText("")
                setAdapter()
            }
        }
    }
}